package com.example.cookie;

import util.CookieUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "CookieServlet", value = "/CookieServlet")
public class CookieServlet extends BaseServlet {
    protected void createCookie(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //      创建Cookie对象
        Cookie cookie = new Cookie("key1", "value1");
//      通知客户端保存
        response.addCookie(cookie);
        response.getWriter().write("cookie创建成功");
    }

    //    服务端获取Cookie
    protected void getCookie(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Cookie[] cookies = request.getCookies();
//        Cookie iWantCookie = null;
//        for (Cookie cookie : cookies) {
//            response.getWriter().write("Cookie[" + cookie.getName() + "]<br>");
//            if("key1".equals(cookie.getName())){
//                iWantCookie = cookie;
//                response.getWriter().write("找到了指定的Cookie key1");
//                break;
//            }
//        }
        Cookie iWantCookie = CookieUtils.findCookie("key1", cookies);
        if (iWantCookie != null) {
            response.getWriter().write("找到了指定的Cookie key1");
        }
    }

    //    修改Cookie
    protected void updateCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        方案一：
//        1、先创建一个要修改的同名的Cookie对象
//         2、在构造器，同时赋于新的Cookie值。
//        Cookie cookie = new Cookie("key1","newValue1");
//        3、调用response.addCookie( Cookie ); 通知 客户端 保存修改
//        resp.addCookie(cookie);

//        方案二：
//        1、先查找到需要修改的Cookie对象
        Cookie cookie = CookieUtils.findCookie("key2", req.getCookies());
        if (cookie != null) {
//            2、调用setValue()方法赋于新的Cookie值。
            cookie.setValue("newValue2");
//        3、调用response.addCookie()通知客户端保存修改
            resp.addCookie(cookie);
        }
        resp.getWriter().write("key1的Cookie已经修改好");
    }

    //    修改Cookie
    protected void defaultCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("defaultLife", "defaultLife");
//        存活时间
//        负数表示关浏览器即删除
        cookie.setMaxAge(-1);
        resp.addCookie(cookie);
    }

    //    删除Cookie
    protected void deleteCookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = CookieUtils.findCookie("key2", req.getCookies());
        if (null == cookie) {
//        0表示删除
            cookie.setMaxAge(0);
            resp.addCookie(cookie);
            resp.getWriter().write("key2 Cookie已删除");
        }
    }


    //    Cookie时间
    protected void life3600Cookie(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("key3","value3");
        //        时间
            cookie.setMaxAge(3600);
            resp.addCookie(cookie);
            resp.getWriter().write("key3 Cookie 生命周期已为3600秒");

    }


    //    Cookie路径  符合路径才发给服务器
    protected void cookiePath(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie cookie = new Cookie("key4","value4");
//        工程路径 + /abc
        cookie.setPath(req.getContextPath() + "/abc");
        resp.addCookie(cookie);
        resp.getWriter().write("key4为不符合路径的Cookie");
        }


}


